package com.study.jsoup;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONReader.Feature;
import com.study.jsoup.Demo01.DataInt.JslDb;
import com.study.jsoup.JslDTO.JslRowCell;
import com.study.jsoup.JslDTO.KzzDTO;
import lombok.Data;
import lombok.experimental.Accessors;
import org.jsoup.Jsoup;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhuzi
 * 2024/7/7 21:50
 */
public class Demo01 {
    @Test
    public void get() {
        String read = FileUtil.readUtf8String("/Users/zhuzi/Downloads/127079.html");
        String s = StrUtil.subBetween(read, "guarantor\"", "</td>");
        System.out.println(s);

    }


    @Test
    public void get1() {
        String body = FileUtil.readUtf8String("/Users/zhuzi/Downloads/sql/kzz.json");
        List<JslRowCell> kzzDTOS = JSON.parseArray(JSON.parseObject(body).getString("rows"), JslRowCell.class, Feature.SupportSmartMatch);

        List<KzzDTO> kzzList = kzzDTOS.stream().map(JslRowCell::getCell)
                .filter(i -> !i.getBondNm().contains("退"))
                .collect(Collectors.toList());

        List<JslDb> collect = kzzList.stream().map(i -> {

            JslDb jslDb = new JslDb();
            jslDb.setBondId(i.getBondId()).setBondNm(i.getBondNm());

            try {
                String body1 = Jsoup.connect(StrUtil.format("https://www.jisilu.cn/data/convert_bond_detail/{}", i.getBondId())).execute().body();
                String s = StrUtil.subBetween(body1, "guarantor\"", "</td>");
                jslDb.setDb(s);
            } catch (IOException e) {
                System.out.println("错误" + i.getBondNm());
            }

            return jslDb;
        }).collect(Collectors.toList());

        System.out.println(JSON.toJSONString(collect));


    }

    @Test
    public void pas() {

        String s = FileUtil.readUtf8String("/Users/zhuzi/Downloads/sql/B-工地/新建.txt");
        List<JslDb> jslDbs = JSON.parseArray(s, JslDb.class);

        jslDbs.stream()
                .peek(i -> i.setDb(i.getDb().replace("colspan=\"7\">", "")))
                .filter(i -> !i.getDb().contains("无") && !i.getDb().contains("质押")).forEach(i -> System.out.println(i.msg()));
    }


    public interface DataInt {
        @Data
        @Accessors(chain = true)
        class JslDb {

            private String bondId;
            @ExcelProperty("名称")
            private String bondNm;

            private String db;

            public String msg() {

                return StrUtil.format("{} {}", bondNm, db);
            }
        }

    }

}
